package com.habdqn.jspdemo.im.dao.impl;

import com.habdqn.jspdemo.im.dao.BaseDao;
import com.habdqn.jspdemo.im.dao.CompanyDao;
import com.habdqn.jspdemo.im.enetity.Company;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 公司数据库操作实现类
 * @author Xing Jiejian
 */
public class CompanyDaoImpl extends BaseDao implements CompanyDao {
    private String baseQuery = "SELECT id,name,city,information,source,updateTime,logo,sourceId FROM company";

    @Override
    public int save(Company company) {
        String sql = "INSERT INTO company(name,city,information,source,logo,sourceId) VALUES(?,?,?,?,?,?)";
        return executeUpdate(sql,company.getName(),company.getCity(),company.getInformation(),company.getSource(),
                company.getLogo(),company.getSourceId());
    }

    @Override
    public int update(Company company) {
        String sql = "UPDATE company SET name=?,city=?,information=?,source=?,updateTime=?,logo=?,sourceId=? WHERE " +
                "id=?";
        Object[] paras = {company.getName(),company.getCity(),company.getInformation(),company.getSource(),
                company.getUpdateTime(),company.getLogo(),company.getSourceId(),company.getId()};
        return executeUpdate(sql,paras);
    }

    @Override
    public int deleteById(int companyId) {
        String sql = "DELETE FROM company WHERE id=?";
        return executeUpdate(sql,companyId);
    }

    @Override
    public Company getById(int companyId) {
        String sql = baseQuery + " WHERE id=?";

        List<Company> companies = getListFromRS(executeQuery(sql,companyId));
        if(companies!=null&&companies.size()>0){
            return companies.get(0);
        }
        return null;
    }

    @Override
    public List<Company> findPageByLikeName(int offset, int pageSize, String likeName) {
        String sql = baseQuery + " WHERE name like ? LIMIT ?,?";
        return getListFromRS(executeQuery(sql,"%"+likeName+"%",offset,pageSize));
    }

    @Override
    public List<Company> findPage(int offset, int pageSize) {
        String sql = baseQuery + " LIMIT ?,?";
        return getListFromRS(executeQuery(sql,offset,pageSize));
    }

    @Override
    public int getCount() {
        String sql = "SELECT COUNT(id) FROM company";
        return (int)(long)uniqueResult(sql);
    }

    @Override
    public int getCountByLikeName(String likeName) {
        String sql = "SELECT COUNT(id) FROM company WHERE name like ?";
        return (int)(long)uniqueResult(sql,"%"+likeName+"%");
    }


    /**
     * 从结果集中提取公司集合
     * @param rs 结果集对象
     * @return 获取公司集合数据
     */
    private List<Company> getListFromRS(ResultSet rs){
        List<Company> companies = null;
        if(rs!=null){
            companies = new ArrayList<>();
            try {
                while (rs.next()){
                    Company company = new Company();
                    company.setId(rs.getInt("id"));
                    company.setName(rs.getString("name"));
                    company.setCity(rs.getString("city"));
                    company.setInformation(rs.getString("information"));
                    company.setSource(rs.getString("source"));
                    company.setUpdateTime(rs.getTimestamp("updateTime"));
                    company.setLogo(rs.getString("logo"));
                    company.setSourceId(rs.getInt("sourceId"));
                    companies.add(company);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                closeAll();
            }
        }
        return companies;
    }
}
